//
// Created by bianzheng on 2022/6/14.
//

#include <string>
#include <vector>
#include <spdlog/spdlog.h>

#include "ConvertRatings.hpp"
#include "StringUtil.hpp"
#include "FileOut.hpp"

using namespace Rec2MIPS;

void ReadFile(const char *input_path, std::vector<RatingString> &rating_str_l) {
    rating_str_l.clear();
    const int n_test = 10;
    for (int testID = 0; testID < n_test; testID++) {

        spdlog::info("read fileID test_{}", testID);
        char input_fname[512];
        sprintf(input_fname, "%s/test_%d.txt", input_path, testID);
        std::ifstream in(input_fname);
        if (!in.is_open()) {
            spdlog::info("error, not open");
            exit(-1);
        }

        std::string line;
        const int report_every_ = 1000000;
        int process_idx = 0;
        while (getline(in, line)) // line中不包括每行的换行符
        {
            if (process_idx++ % report_every_ == 0) {
                spdlog::info("read idx yahoomusicbig {}", process_idx);
            }
            std::vector<std::string> arr_str = Rec2MIPS::split(line, '\t');
            if (arr_str.size() < 3) {
                continue;
            }
            std::string user_id = arr_str[0];
            std::string item_id = arr_str[1];
            std::string rating = arr_str[2];
            rating_str_l.emplace_back(user_id, item_id, rating);
        }
        in.close();
    }

    const int n_train = 10;
    for (int trainID = 0; trainID < n_train; trainID++) {

        spdlog::info("read fileID train_{}", trainID);
        char input_fname[512];
        sprintf(input_fname, "%s/train_%d.txt", input_path, trainID);
        std::ifstream in(input_fname);
        if (!in.is_open()) {
            spdlog::info("error, not open");
            exit(-1);
        }

        std::string line;
        const int report_every_ = 1000000;
        int process_idx = 0;
        getline(in, line); //skip the first line, it is the head
        while (getline(in, line)) // line中不包括每行的换行符
        {
            if (process_idx++ % report_every_ == 0) {
                spdlog::info("read idx yahoomusicbig {}", process_idx);
            }
            std::vector<std::string> arr_str = Rec2MIPS::split(line, '\t');
            if (arr_str.size() < 3) {
                continue;
            }
            std::string user_id = arr_str[0];
            std::string item_id = arr_str[1];
            std::string rating = arr_str[2];
            rating_str_l.emplace_back(user_id, item_id, rating);
        }
        in.close();
    }

}

using namespace std;

int main(int argc, char **argv) {
    if (argc != 3) {
        spdlog::info("input path, output filename");
        return 0;
    }
    char *input_path = argv[1];
    char *output_fname = argv[2];
    spdlog::info("input path {}, output filename {}", input_path, output_fname);

    vector<RatingString> rating_str_l;
    ReadFile(input_path, rating_str_l);

    std::vector<RatingNum> rating_num_l;
    pair<int, int> data_stat = ConvertRatings(rating_str_l, rating_num_l);

    spdlog::info("YahooMusicBig total n_user {}, total n_item {}", data_stat.first, data_stat.second);

    WriteFile(output_fname, "Yahoomusic big", rating_num_l);
    return 0;
}